<?php
/**
 * @Author    紫梦
 * @DateTime  2018-02-02
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\Cars;
use app\common\controller\Execl;
use PHPExcel_IOFactory;
use PHPExcel;
class Excell extends Base
{
	public $excel;

	public function cars()
	{
		$request = Request::instance();
		$get = $request->get();
		$datemin = $get['datemin']??'';
        $datemax = $get['datemax']??'';
        $seach = $get['search']??'';
		$relevance = $get['relevance']??'';
        $agency = $get['agency_id']??'';
        $brand = $get['brand_id']??'';

        $PHPExcel = new \PHPExcel();
        $PHPSheet = $PHPExcel->getActiveSheet();
        $PHPSheet->setTitle('车辆基础数据');
        $this->excel = $PHPExcel;
        //填入表头
        $PHPExcel->getActiveSheet()->setCellValue('A1', '车牌号码');
        $PHPExcel->getActiveSheet()->setCellValue('B1', '车架号');
        $PHPExcel->getActiveSheet()->setCellValue('C1', '发动机号');
        $PHPExcel->getActiveSheet()->setCellValue('D1', '车辆型号');
        $PHPExcel->getActiveSheet()->setCellValue('E1', '汽车品牌');
        $PHPExcel->getActiveSheet()->setCellValue('F1', '车辆类型');
        $PHPExcel->getActiveSheet()->setCellValue('G1', '供应商ID');
        $PHPExcel->getActiveSheet()->setCellValue('H1', '物联网卡号');
        $PHPExcel->getActiveSheet()->setCellValue('I1', '物联网卡序列号');
        $PHPExcel->getActiveSheet()->setCellValue('J1', '车载设备编号');
        $PHPExcel->getActiveSheet()->setCellValue('K1', '副供应商ID');
        $PHPExcel->getActiveSheet()->setCellValue('L1', '副物联网卡号');
        $PHPExcel->getActiveSheet()->setCellValue('M1', '副物联网卡序列号');
        $PHPExcel->getActiveSheet()->setCellValue('N1', '副车载设备编号');
        $PHPExcel->getActiveSheet()->setCellValue('O1', '租赁类型');
        $PHPExcel->getActiveSheet()->setCellValue('P1', '机车所有人');
        $PHPExcel->getActiveSheet()->setCellValue('Q1', '所属加盟商');

		if( !empty($datemin) && !empty($datemax) ){
            $where['create_time'] = ['between time',[$datemin,$datemax]];	               
                
        }
        if ( empty($datemin) && !empty($datemax) ){	            
            $where['create_time'] =['<= time',$datemax];
            	
            	
        }
        if ( !empty($datemin) && empty($datemax) ) {
        	$where ['create_time'] = ['> time',$datemin];
             
        }
        if ( !empty($seach) ) {
        	$where['car_sn|car_unique_id|cardcode|equipment_id'] = ['like','%'.$seach.'%'];
                
        }
        if ( !empty($relevance) ) {
        	$where['relevance'] = $relevance;     
                
        }
        if ( !empty($agency) ) {
        	$where ['agency_id'] = $agency;
                
        }
        if ( !empty($brand) ) {
        	$where ['brand_id'] = $brand;
                
        }
        
        if( empty($where) ){
        	$where = '1=1';
        }    
		try {			
			Cars::where($where)->chunk(100,function($model){
				$car_breed=['小轿车','SUV','客车'];
				$supplier_no = ['1376920002'=>'博鹭途','1376920001'=>'唯特迅'];
				$relevance = ['未知','分时租赁','企业用车','共享用车','长租车辆','自测试用','上牌整备','整备待租'];
				static $num = 1;
			    	
			    foreach($model as $cars){
			   
			    	$num ++;
					$this->excel->getActiveSheet()->setCellValue("A".$num."","".$cars->car_sn."");
					$this->excel->getActiveSheet()->setCellValue("B".$num."","".$cars->car_unique_id."");
					$this->excel->getActiveSheet()->setCellValue("C".$num."","".$cars->motor_id."");
					$this->excel->getActiveSheet()->setCellValue("D".$num."","".$cars->car_name."");
					$this->excel->getActiveSheet()->setCellValue("E".$num."","".$cars->brand_name."");
					$this->excel->getActiveSheet()->setCellValue("F".$num."","".$car_breed[$cars->car_breed]."");
			    	if( in_array( $cars->supplier_no, array_keys($supplier_no) ) ){
						$this->excel->getActiveSheet()->setCellValue("G".$num."","".$supplier_no[$cars->supplier_no]."");

			    	}
			    	$this->excel->getActiveSheet()->setCellValue("H".$num."","".$cars->cardcode."");
			    	$this->excel->getActiveSheet()->setCellValue("I".$num."","".$cars->sim_id."");
			    	$this->excel->getActiveSheet()->setCellValue("J".$num."","".$cars->equipment_id."");
			    	$this->excel->getActiveSheet()->setCellValue("O".$num."","".$relevance[$cars->relevance??0]."");
			    	$this->excel->getActiveSheet()->setCellValue("P".$num."","".$cars->holder_name."");
			    	$this->excel->getActiveSheet()->setCellValue("Q".$num."","".$cars->Agency_name."");

			    }
			});			
		} catch (\Exception $e) {
			
			
				echo $e->getMessage();
		}

		$PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
        header('Content-Disposition: attachment;filename="'.date('Y-m-d',time()).'车辆基础数据.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output");exit;
		
	}
}